package leetcode.map;

public class Lc695 {

    final int[] X = {-1, 1, 0, 0};
    final int[] Y = {0, 0, -1, 1};
    int m;
    int n;

    public int maxAreaOfIsland(int[][] grid) {
        int ans = 0;
        m = grid.length;
        n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    ans = Math.max(ans, dfs(grid, i, j, 0));
                }
            }
        }
        return ans;
    }

    private int dfs(int[][] grid, int x, int y, int cnt) {
        int ans = 1;
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nx = x + X[i];
            int ny = y + Y[i];
            if (check(nx, ny) && grid[nx][ny] == 1)
                ans += dfs(grid, nx, ny, cnt);
        }
        return ans;
    }

    private boolean check(int x, int y) {
        if (x < 0 || x >= m || y < 0 || y >= n) return false;
        return true;
    }

}
